package com.test.fundible.fundible_08_loan_repay;

import static com.test.fundible.fundible_00_utils.FundibleUtil.startLoading;
import static com.test.fundible.fundible_00_utils.FundibleUtil.stopLoading;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.test.fundible.R;
import com.test.fundible.fundible_07_loan_status.Base_fundibleLnFragment;
import com.test.fundible.fundible_04_home.LnLi_fundiblemitInfo;
import com.test.fundible.Othe_fundiblersInfo;
import com.test.fundible.fundible_00_http.OkHttpUtil;
import com.test.fundible.fundible_00_utils.FundibleUtil;

public class Ln_fundibleRpyFr_fundibleagment extends Base_fundibleLnFragment {

    private TextView homeAmountTv;
    private TextView totalRepaymentTv;
    private TextView dueDateTv;
    private TextView daysRemainingTv;
    private TextView loanAmountTv;
    private TextView interestTv;
    private TextView serviceFeeTv;
    private TextView overdueDaysTv;
    private TextView overduePenaltyTv;
    private View foldView;
    private ImageButton foldIb;
    private Button repayBtn;

    private View vatView;
    private TextView vatTv;

    private Runnable refreshRunnable = null;

    private static boolean refreshPage = false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fundible_loan_rpy, container, false);

        homeAmountTv = view.findViewById(R.id.fflr_tv_home_amount);
        totalRepaymentTv = view.findViewById(R.id.fflr_tv_total_repayment);
        dueDateTv = view.findViewById(R.id.fflr_tv_due_date);
        daysRemainingTv = view.findViewById(R.id.fflr_tv_days_remaining);
        loanAmountTv = view.findViewById(R.id.fflr_tv_loan_amount);
        interestTv = view.findViewById(R.id.fflr_tv_interest);
        serviceFeeTv = view.findViewById(R.id.fflr_tv_service_fee);
        vatView = view.findViewById(R.id.fflr_view_vat);
        vatTv = view.findViewById(R.id.fflr_tv_vat);
        overdueDaysTv = view.findViewById(R.id.fflr_tv_overdue_days);
        overduePenaltyTv = view.findViewById(R.id.fflr_tv_overdue_penalty);
        foldView = view.findViewById(R.id.fflr_view_fold);
        vatView = view.findViewById(R.id.fflr_view_vat);
        foldIb = view.findViewById(R.id.fflr_ib_fold);
        repayBtn = view.findViewById(R.id.fflr_btn_repay);

        SwipeRefreshLayout refreshLayout = view.findViewById(R.id.fflr_refreshlayout);
        refreshLayout.setOnRefreshListener(() -> {

            if (refreshPage && refreshRunnable!=null) {
                refreshPage = false;
                refreshRunnable.run();
                return;
            }

            refreshLayout.setRefreshing(false);
            startLoading(requireContext(),"");
            OkHttpUtil.getInstance_fundible().uploadEventPoint("REPAY","REFRESH_PULL","");
            OkHttpUtil.getInstance_fundible().fetchRpDetail(this::refreshHomeLimit);

        });


        init_fundible();

        return view;
    }

    private void init_fundible(){
        //折叠/展开
        foldIb.setOnClickListener(v -> fold());

        repayBtn.setOnClickListener(v -> {
            startLoading(requireContext(),"");
            OkHttpUtil.getInstance_fundible().uploadEventPoint("REPAY","SUBMIT_CLICK","");
            if (FundibleUtil.isOrderRepayH5_fundible()) OkHttpUtil.getInstance_fundible().odRpH5Path("NO",this::toPay_fundible);
            else if (FundibleUtil.isGetOrderRepayH5_fundible()) OkHttpUtil.getInstance_fundible().getOdRpH5Path("NO",this::toPay_fundible);
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        startLoading(requireContext(),"");
        OkHttpUtil.getInstance_fundible().uploadEventPoint("REPAY","START","");

        if (refreshPage && refreshRunnable!=null) {
            refreshPage = false;
            refreshRunnable.run();
            return;
        }

        OkHttpUtil.getInstance_fundible().requestHomeAmountList(this::refreshHomeLimit);
    }

    private void fold(){

        if (foldView.getVisibility() == View.VISIBLE){
            foldView.setVisibility(View.GONE);
            foldIb.setImageResource(R.drawable.icon_detail_fold);
        }else {
            foldView.setVisibility(View.VISIBLE);
            foldIb.setImageResource(R.drawable.icon_detail_unfold);
        }

    }

    @SuppressLint("SetTextI18n")
    public void refreshHomeLimit(){

        homeAmountTv.setText(LnLi_fundiblemitInfo.getInstance_fundible().getLimitAmount_fundible()+" "+ LnLi_fundiblemitInfo.getInstance_fundible().getCurrency_fundible());

        try {
            OkHttpUtil.getInstance_fundible().fetchRpDetail(this::refreshDetails_fundible);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @SuppressLint("SetTextI18n")
    public void refreshDetails_fundible(){

        stopLoading();

        String currency = RpI_fundiblenf_fundibleo.getInstance().getCurrency_fundible();

        totalRepaymentTv.setText(RpI_fundiblenf_fundibleo.getInstance().getRepayAmount_fundible()+" "+currency);
        dueDateTv.setText(RpI_fundiblenf_fundibleo.getInstance().getRepayDate_fundible());
        loanAmountTv.setText(RpI_fundiblenf_fundibleo.getInstance().getAmount_fundible()+" "+currency);
        interestTv.setText(RpI_fundiblenf_fundibleo.getInstance().getInterest_fundible()+" "+currency);
        daysRemainingTv.setText(RpI_fundiblenf_fundibleo.getInstance().getRemainDay());
        overdueDaysTv.setText(RpI_fundiblenf_fundibleo.getInstance().getOverdueDay_fundible());
        overduePenaltyTv.setText(RpI_fundiblenf_fundibleo.getInstance().getOverdueAmount_fundible()+" "+currency);

        if (FundibleUtil.isCote_fundible()){
            vatView.setVisibility(View.VISIBLE);
            vatTv.setText(RpI_fundiblenf_fundibleo.getInstance().getServiceVatFee_fundible()+" "+currency);
        }else vatView.setVisibility(View.GONE);

        if (RpI_fundiblenf_fundibleo.getInstance().isOvdu_fundible()) overdueDaysTv.setTextColor(Color.RED);
        else overdueDaysTv.setTextColor(Color.parseColor("#FF666666"));

        if (FundibleUtil.isFrench_fundible()) serviceFeeTv.setText(RpI_fundiblenf_fundibleo.getInstance().getRepayProFee_fundible()+" "+currency);
        else serviceFeeTv.setText(RpI_fundiblenf_fundibleo.getInstance().getServiceFee_fundible()+" "+currency);

    }

    public void toPay_fundible(){
        stopLoading();
        String path = Othe_fundiblersInfo.getInstance_fundible().getRpPath_fundible();

        if (!TextUtils.isEmpty(path)) {
            refreshPage = true;
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(path)));
        }
    }
    
    @Override
    public void onPause() {
        super.onPause();
        OkHttpUtil.getInstance_fundible().uploadEventPoint("REPAY","END","");
    }

    public void setRefreshRunnable(Runnable refreshRunnable) {
        this.refreshRunnable = refreshRunnable;
    }
}